package com.example.blog.controller;

import com.example.blog.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Value("${deepseek.api.key}")
    private String apiKey;

    private final RestTemplate restTemplate = new RestTemplate();

    @PostMapping("/completion")
    public Result<Object> chatCompletion(@RequestBody Map<String, Object> request) {
        String url = "https://api.deepseek.com/chat/completions";
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + apiKey);
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
        
        try {
            Object response = restTemplate.postForObject(url, entity, Object.class);
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("调用AI接口失败: " + e.getMessage());
        }
    }
} 